#include "test_eloop.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "string.h"

/*
CM:ELOOP_OPERATION:DEFAULT_ELOOP    //查询系统默认的eloop
CM:ELOOP_OPERATION:CREAT_ELOOP       //创建新的eloop
CM:ELOOP_OPERATION:DELETE_ELOOP:[eloop地址]       //删除创建的eloop
CM:ELOOP_OPERATION:WAIT_EVENT:[eloop地址]:[超时时间]        //等待事件触发
CM:ELOOP_OPERATION:REG_EVENT:[eloop地址]             //在某个eloop中注册事件   返回事件地址

CM:ELOOP_OPERATION:POST_EVENT:[事件地址]       //发送事件到eloop，，触发某个事件？？
CM:ELOOP_OPERATION:UNREG_EVENT:[event地址]        //取消loop中的事件

*/


static void test_event_eloop_cb(cm_eloop_event_handle_t event,void *cb_param)
{
    cm_demo_printf("event:%p, cb_param:%s\n",event,cb_param);
}
void cm_test_eloop_operation(unsigned char*cmd[],int len)
{
    if(0 == strncmp("INIT",cmd[2],strlen("INIT")))
    {
        //cm_demo_printf("cm_eloop_init ret:%d\n",cm_eloop_init());
    }else if(0 == strncmp("DEFAULT_ELOOP",cmd[2],strlen("DEFAULT_ELOOP")))
    {
        cm_eloop_handle_t eloop_default = NULL;
        eloop_default = cm_eloop_default();
        cm_demo_printf("cm_eloop_default ret:%p\n",eloop_default);
        OCTEST_FUNC_EXPECT_NOT_NULL(eloop_default,cm_eloop_default);
    }
    else if(0 == strncmp("CREAT_ELOOP",cmd[2],strlen("CREAT_ELOOP")))
    {
        cm_eloop_handle_t eloop_creat = NULL;
        eloop_creat = cm_eloop_create(20);
        cm_demo_printf("cm_eloop_create ret:%p\n",eloop_creat);
        OCTEST_FUNC_EXPECT_NOT_NULL(eloop_creat,cm_eloop_create);
    }else if(0 == strncmp("DELETE_ELOOP",cmd[2],strlen("DELETE_ELOOP")))
    {
        cm_eloop_handle_t eloop = strtoul(cmd[3],(void**)0,16);
        cm_eloop_delete(eloop);
        cm_demo_printf("cm_eloop_delete no return\n");
    }else if(0 == strncmp("WAIT_EVENT",cmd[2],strlen("WAIT_EVENT")))
    {
        cm_eloop_handle_t eloop = strtoul(cmd[3],(void**)0,16);
        uint32_t timeout = strtoul(cmd[4],(void**)0,10);
        int32_t wait_event = cm_eloop_wait_event(eloop, timeout);
        cm_demo_printf("cm_eloop_wait_event ret:%d\n", wait_event);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,wait_event,cm_eloop_wait_event);
    
    }else if(0 == strncmp("REG_EVENT",cmd[2],strlen("REG_EVENT")))
    {
        cm_eloop_handle_t eloop = strtoul(cmd[3],(void**)0,16);
        cm_eloop_event_handle_t register_event = NULL;
        register_event = cm_eloop_register_event(eloop, test_event_eloop_cb, "reg event");
        cm_demo_printf("cm_eloop_register_event ret:%p\n",register_event);
        OCTEST_FUNC_EXPECT_NOT_NULL(register_event,cm_eloop_register_event);
    }
    else if(0 == strncmp("UNREG_EVENT",cmd[2],strlen("UNREG_EVENT")))
    {
        cm_eloop_event_handle_t event = strtoul(cmd[3],(void**)0,16);
        cm_eloop_unregister_event(event);
        cm_demo_printf("cm_eloop_unregister_event no return\n");
    }else if(0 == strncmp("POST_EVENT",cmd[2],strlen("POST_EVENT")))
    {
        cm_eloop_event_handle_t event = strtoul(cmd[3],(void**)0,16);
        int32_t post_event = cm_eloop_post_event(event);
        cm_demo_printf("cm_eloop_register_event ret:%d\n",post_event);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,post_event,cm_eloop_post_event);
    }
    else if(0 == strncmp("HEAP",cmd[2],strlen("HEAP")))
    {
        cm_eloop_handle_t eloop_default = NULL;
        eloop_default = cm_eloop_default();
        cm_demo_printf("cm_eloop_default ret:%p\n",eloop_default);
        OCTEST_FUNC_EXPECT_NOT_NULL(eloop_default,cm_eloop_default);

        cm_eloop_handle_t eloop_creat = NULL;
        eloop_creat = cm_eloop_create(20);
        cm_demo_printf("cm_eloop_create ret:%p\n",eloop_creat);
        OCTEST_FUNC_EXPECT_NOT_NULL(eloop_creat,cm_eloop_create);

        cm_eloop_event_handle_t register_event = NULL;
        register_event = cm_eloop_register_event(eloop_creat, test_event_eloop_cb, "reg event");
        cm_demo_printf("cm_eloop_register_event ret:%p\n",register_event);

        int32_t post_event = cm_eloop_post_event(register_event);
        cm_demo_printf("cm_eloop_register_event ret:%d\n",post_event);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,post_event,cm_eloop_post_event);

        int32_t wait_event = cm_eloop_wait_event(eloop_creat, 10000);
        cm_demo_printf("cm_eloop_wait_event ret:%d\n", wait_event);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(1,wait_event,cm_eloop_wait_event);

        wait_event = cm_eloop_wait_event(eloop_creat, 1000);
        cm_demo_printf("cm_eloop_wait_event ret:%d\n", wait_event);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,wait_event,cm_eloop_wait_event);

        osDelay(5);
        cm_eloop_unregister_event(register_event);
        cm_demo_printf("cm_eloop_unregister_event no return\n");

        cm_eloop_delete(eloop_creat);
        cm_demo_printf("cm_eloop_delete no return\n");
        cm_heap_stats_t stats = {0};
        int ret = cm_mem_get_heap_stats(&stats);
        if(0 == ret)
        {
            cm_demo_printf("cm_mem_get_heap_stats RET:%d\n",ret);
            cm_demo_printf("[mem]Total Size:%d,Free Size:%d,Allocated Size:%d\n",stats.total_size,stats.free,stats.allocated);
        }
        else
        {
            cm_demo_printf("[mem]Error:%d\n",ret);
        }
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,ret,cm_mem_get_heap_stats);
    }
    else{
        cm_demo_printf("param error!\n");
    }
}
